﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLBH.DataModel;
using QLBH.Base;
using QLBH.Model.Controller;
using QLBH.ModelInterface;

namespace QLBH.FeatureModel
{
    public class QLMatHangModel:IQLMatHangModel
    {
        public Guid MHID { get; set; }
        public String MHCODE { get; set; }
        public String MHTEN { get; set; }
        public Decimal SOLUONG { get; set; }
        public Decimal GIANHAP { get; set; }
        public Decimal GIABANLE { get; set; }
        public Decimal GIABANBUON { get; set; }
        public DateTime NGAYDCGIANHAP { get; set; }
        public DateTime NGAYDCGIABANLE { get; set; }
        public DateTime NGAYDCGIABANBUON { get; set; }
        public Decimal CHIETKHAU { get; set; }
        public String XUATXU { get; set; }
        public String GHICHU { get; set; }
        public Boolean THEODOI { get; set; }
        public Guid NHOMHANGID { get; set; }
        public String TENNHOMHANG { set; get; }
        public Guid DVTID { get; set; }
        public String TENDVT { set; get; }
        public Guid NCCID { set; get; }
        public String TENNCC { set; get; }

        public void InitFromEntity(MATHANG entity)
        {
            MHID            = entity.MHID;
            MHCODE          = entity.MHCODE;
            MHTEN           = entity.MHTEN;
            SOLUONG         = (Decimal)entity.SOLUONG;
            GIANHAP         = (Decimal)entity.GIANHAP;
            GIABANLE        = (Decimal)entity.GIABANLE;
            GIABANBUON      = (Decimal)entity.GIABANBUON;
            NGAYDCGIANHAP   = (DateTime)entity.NGAYDCGIANHAP;
            NGAYDCGIABANLE  = (DateTime)entity.NGAYDCGIABANLE;
            NGAYDCGIABANBUON = (DateTime)entity.NGAYDCGIABANBUON;            
            XUATXU          = entity.XUATXU;
            GHICHU          = entity.GHICHU;
            THEODOI         = (bool)entity.THEODOI;
            NHOMHANGID      = (Guid)entity.NHOMHANGID;
            DVTID           = (Guid)entity.DVTID;
            NCCID           = (Guid)entity.NCCID;

            var NHOMHANG = NhomHangController.GetInstance().FindBy(NH => NH.LOAIHANGID == entity.NHOMHANGID).FirstOrDefault();
            if(NHOMHANG!=null)
            {
                TENNHOMHANG = NHOMHANG.TENLOAIHANG;
            }
            var NCC = NCCController.GetInstance().FindBy(ncc => ncc.NCCUID == entity.NCCID).FirstOrDefault();
            if (NCC != null)
            {
                TENNCC = NCC.TENNCC;
            }
            var dvt = DVTController.GetInstance().FindBy(vt => vt.DVTID == entity.DVTID).FirstOrDefault();
            if (dvt != null)
            {
                TENDVT = dvt.TENDVT;
            }
            var ncc_mh = NCC_MatHangController.GetInstance().FindBy(nccmh => nccmh.MHID == entity.MHID).FirstOrDefault();
            if (ncc_mh!=null)
            {
                CHIETKHAU = (Decimal)ncc_mh.CHIETKHAU;
            }
        }


        public MATHANG ToEntity()
        {
            MATHANG entity = new MATHANG();
            entity.DVTID            = DVTID;
            entity.GHICHU           = GHICHU;
            entity.GIABANBUON       = GIABANBUON;
            entity.GIABANLE         = GIABANLE;
            entity.GIANHAP          = GIANHAP;
            entity.MHCODE           = MHCODE;
            entity.MHID             = MHID;
            entity.MHTEN            = MHTEN;
            entity.NCCID            = NCCID;
            entity.NGAYDCGIABANBUON = NGAYDCGIABANBUON;
            entity.NGAYDCGIABANLE   = NGAYDCGIABANLE;
            entity.NGAYDCGIANHAP    = NGAYDCGIANHAP;
            entity.NHOMHANGID       = NHOMHANGID;
            entity.SOLUONG          = SOLUONG;
            entity.THEODOI          = THEODOI;            
            entity.XUATXU           = XUATXU;            

            return entity;
        }
    }
}
